using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.ServiceManager
{
    public class CodeManager
    {
        #region CONSTRUCTORS
        static CodeManager()
        {
            _data = new CodeData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static CodeData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(string codeName, string codeValue, string contents, string codeType, string info)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = Util.ReplaceNullTo(codeName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(codeValue, DBNull.Value);
                values[2] = Util.ReplaceNullTo(contents, DBNull.Value);
                values[3] = Util.ReplaceNullTo(codeType, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(CodeInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = Util.ReplaceNullTo(info.CodeName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(info.CodeValue, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.Contents, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.CodeType, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.Info, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(int codeID, string codeName, string codeValue, string contents, string codeType, string info)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = codeID;
                values[1] = Util.ReplaceNullTo(codeName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(codeValue, DBNull.Value);
                values[3] = Util.ReplaceNullTo(contents, DBNull.Value);
                values[4] = Util.ReplaceNullTo(codeType, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(CodeInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = info.CodeID;
                values[1] = Util.ReplaceNullTo(info.CodeName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.CodeValue, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.Contents, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.CodeType, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.Info, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(int codeID)
        {
            _error = "";
            try
            {
                object[] values = { codeID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(CodeInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.CodeID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static CodeInfo Select(int codeID)
        {
            _error = "";
            try
            {
                object[] values = { codeID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                CodeInfo info = new CodeInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable GetCode(string CodeName)
        {
            object[] values = new object[1];
            values[0] = CodeName;

            return _data.GetCode(values);
        }

        public static CodeInfo GetCodeDetail(string CodeType, string CodeName, string CodeValue)
        {
            object[] values = new object[3];
            values[0] = CodeType;
            values[1] = CodeName;
            values[3] = CodeValue;

            DataTable dt = _data.GetCodeDetail(values);

            if (dt.Rows.Count < 1)
                return null;

            DataRow dr = dt.Rows[0];

            CodeInfo objCodeInfo = new CodeInfo();

            objCodeInfo.CodeType = dr["CodeType"].ToString();
            objCodeInfo.CodeName = dr["CodeName"].ToString();
            objCodeInfo.CodeValue = dr["CodeValue"].ToString();
            objCodeInfo.Contents = dr["Contents"].ToString();
            objCodeInfo.Info = dr["Info"].ToString();
            return objCodeInfo;
        }

        public static CodeInfo GetCodeDetail(string CodeType, string CodeName)
        {
            object[] values = new object[2];
            values[0] = CodeType;
            values[1] = CodeName;

            DataTable dt = _data.GetCodeDetailTable(values);

            if (dt.Rows.Count < 1)
                return null;

            DataRow dr = dt.Rows[0];

            CodeInfo objCodeInfo = new CodeInfo();

            objCodeInfo.CodeType = dr["CodeType"].ToString();
            objCodeInfo.CodeName = dr["CodeName"].ToString();
            objCodeInfo.CodeValue = dr["CodeValue"].ToString();
            objCodeInfo.Contents = dr["Contents"].ToString();
            objCodeInfo.Info = dr["Info"].ToString();
            return objCodeInfo;
        }

        public static DataTable GetCodeDetailTable(string CodeType, string CodeName)
        {
            object[] values = new object[2];
            values[0] = CodeType;
            values[1] = CodeName;

            DataTable dt = _data.GetCodeDetailTable(values);
            if (dt.Rows.Count < 1) return null;
            else return dt;
        }

        public static string GetContent(string CodeName, string CodeValue)
        {
            object[] values = new object[2];
            values[0] = CodeName;
            values[1] = CodeValue;

            DataTable dt = _data.GetContent(values);

            if (dt.Rows.Count < 1)
                return "";

            DataRow dr = dt.Rows[0];
            return dr["Contents"].ToString();
        }

        #endregion

    }
}